package leetcode.weekly.week317;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

//Solution2Test
public class Solution5 {

	public List<List<String>> mostPopularCreator(String[] creators, String[] ids, int[] views) {
		int n = creators.length;
		Map<String, Long> total = new HashMap<>();
		long max = 0;
		for (int i = 0; i < n; i++) {
			String c = creators[i];
			long cur = total.getOrDefault(c, 0l) + 1l * views[i];
			max = Math.max(max, cur);
			total.put(c, cur);
		}
		Set<String> cs = new HashSet<>();
		Map<String, Map<String, Long>> map = new HashMap<>();
		for (Map.Entry<String, Long> c : total.entrySet()) {
			if (c.getValue() == max) {
				cs.add(c.getKey());
				map.put(c.getKey(), new HashMap<>());
			}
		}
		for (int i = 0; i < n; i++) {
			String c = creators[i];
			if (cs.contains(c)) {
				String id = ids[i];
				int view = views[i];
				Map<String, Long> m = map.get(c);
				m.put(id, m.getOrDefault(id, 0l) + 1l * view);
				map.put(c, m);
			}
		}
		List<List<String>> ans = new ArrayList<>();
		for (Map.Entry<String, Map<String, Long>> c : map.entrySet()) {
			String name = c.getKey();
			long m = -1;
			String maxid = null;
			for (Map.Entry<String, Long> cc : c.getValue().entrySet()) {
				String id = cc.getKey();
				long view = cc.getValue();
				if (view > m) {
					m = view;
					maxid = id;
				} else if (view == m && id.compareTo(maxid) < 0) {
					m = view;
					maxid = id;
				}
			}
			ans.add(Arrays.asList(new String[] { name, maxid }));
		}
		return ans;
	}
	
	public static void main(String[] args) {
		String a = "a";
		String b = "a";
		System.out.println(b.compareTo(a));
		System.out.println(a.compareTo(b));
	}

}
